
var mysql = require("mysql");
var { createConnection } = mysql;

// node连接mysql 选择数据库
var conn = createConnection({
    host: "localhost",
    port: 3306,
    user: "root",
    password: "root",
    database: "2213",
})

// conn.connect();  开启链接
// conn.end();      关闭链接

// 执行sql语句
// 查询(单)
// conn.query("select id,name,class,chinese,math,english,chinese+math+english as total from grade where id = 1", function (err, result, field) {
//     if (err) throw err;
//     console.log(result); // [{id,name,class,chinese,math,english,total}]
//     // console.log(field);
// })

// conn.query("select id,name,class,chinese,math,english,chinese+math+english as total from grade", function (err, result, field) {
//     if (err) throw err;
//     console.log(result); // [{id,name,class,chinese,math,english,total}]
//     // console.log(field);
// })

// conn.query("update grade set chinese = 90, math = 100,english = 0 where id = 120", function (err, result) {
//     if (err) throw err;
//     console.log(result);
//     conn.end();
// })

conn.connect();   //查询之前开启链接  
conn.query("update grade set chinese = 90, math = 100,english = 0 where id = 120", function (err, result) {
    if (err) throw err;
    console.log(result);
    conn.end(); // 查询之后关闭链接
})





